package com.vinci.config;

import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableTransactionManagement    //自动管理事物
@MapperScan("com.vinci.mapper")  //扫描mapper文件夹
@Configuration  //配置类
public class MybatisPlusConfig {
@Bean
    //注册乐观锁插件
    public OptimisticLockerInterceptor optimisticLockerInterceptor(){
        return new OptimisticLockerInterceptor();
    }

    //分页插件
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
       return paginationInterceptor;
    }

    //逻辑删除
    @Bean
    public ISqlInjector sqlInjector(){
    return new LogicSqlInjector();
    }

    //测试sql执行效率插件
    @Bean
    @Profile({"dev","test"})    //设置dev(开发环境) test(测试环境)环境开启，保证我们的效率
    public PerformanceInterceptor performanceInterceptor(){
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        //设置sql执行的最大世界
        performanceInterceptor.setMaxTime(1000);   //ms 如果sql运行时间超过就不执行
        performanceInterceptor.setFormat(true);
        return performanceInterceptor;
    }

}
